package cn.bingo.yunsj.service;

import cn.bingo.yunsj.common.StateCode;
import cn.bingo.yunsj.common.result.Response;
import cn.bingo.yunsj.controller.model.RpSuccessAndFaile;
import cn.bingo.yunsj.controller.model.SerachModel;
import cn.bingo.yunsj.domain.RpNotSend;
import cn.bingo.yunsj.domain.RpSendFail;
import cn.bingo.yunsj.domain.RpSuccess;
import cn.bingo.yunsj.mapper.RpNotSendMapper;
import cn.bingo.yunsj.mapper.RpSendFailMapper;
import cn.bingo.yunsj.mapper.RpSuccessMapper;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class SendAllService {

    @Autowired
    private RpSendFailMapper rpSendFailMapper;

    @Autowired
    private RpSuccessMapper rpSuccessMapper;

    public Response all() {

        List<RpSuccess> rpSuccesses = rpSuccessMapper.selectAll();

        List<RpSendFail> rpSendFails = rpSendFailMapper.selectAll();

        List<RpSuccessAndFaile> rpSuccessAndFailes = new ArrayList<>();

        rpSendFails.forEach(e->{
            RpSuccessAndFaile rpSuccessAndFaile = new RpSuccessAndFaile(){{
                setUserModel(e.getUserModel());
                setRecipient(e.getRecipient());
                setSender(e.getSender());
                setUpdatedBy(e.getUpdatedBy());
                setSendTime(e.getSendTime());
                setState("失败");
                setJoinTime(e.getJoinTime());
                setDepartureTime(e.getDepartureTime());
                setFailId(e.getFailId());
            }};
            rpSuccessAndFailes.add(rpSuccessAndFaile);
        });

        rpSuccesses.forEach(e->{
            RpSuccessAndFaile rpSuccessAndFaile = new RpSuccessAndFaile(){{
                setUserModel(e.getUserModel());
                setRecipient(e.getRecipient());
                setSender(e.getSender());
                setUpdatedBy(e.getUpdatedBy());
                setSendTime(e.getSendTime());
                setJoinTime(e.getJoinTime());
                setDepartureTime(e.getDepartureTime());
                setState("成功");
            }};
            rpSuccessAndFailes.add(rpSuccessAndFaile);
        });

        return new Response(StateCode.FIND_SUCCESS, (long) rpSuccessAndFailes.size(),rpSuccessAndFailes);
    }

    public Response search(SerachModel serachModel, String start, String end) {

        if(serachModel.getUserModel()==null){
            serachModel.setUserModel("");
        }

        List<RpSuccess> rpSuccesses = rpSuccessMapper.search(serachModel.getUserModel(), start, end);

        List<RpSendFail> rpSendFail = rpSendFailMapper.search(serachModel.getUserModel(), start, end);

        List<RpSuccessAndFaile> rpSuccessAndFailes = new ArrayList<>();

        rpSendFail.forEach(e->{
            RpSuccessAndFaile rpSuccessAndFaile = new RpSuccessAndFaile(){{
                setUserModel(e.getUserModel());
                setRecipient(e.getRecipient());
                setSender(e.getSender());
                setUpdatedBy(e.getUpdatedBy());
                setSendTime(e.getSendTime());
                setJoinTime(e.getJoinTime());
                setDepartureTime(e.getDepartureTime());
                setState("失败");
                setFailId(e.getFailId());
            }};
            rpSuccessAndFailes.add(rpSuccessAndFaile);
        });

        rpSuccesses.forEach(e->{
            RpSuccessAndFaile rpSuccessAndFaile = new RpSuccessAndFaile(){{
                setUserModel(e.getUserModel());
                setRecipient(e.getRecipient());
                setSender(e.getSender());
                setUpdatedBy(e.getUpdatedBy());
                setSendTime(e.getSendTime());
                setJoinTime(e.getJoinTime());
                setDepartureTime(e.getDepartureTime());
                setState("成功");
            }};
            rpSuccessAndFailes.add(rpSuccessAndFaile);
        });
        return new Response(StateCode.SEARCH_SUCCESS, (long) rpSuccessAndFailes.size(),rpSuccessAndFailes);
    }

    public Response getType() {
        List<RpSendFail> rpSendFails = rpSendFailMapper.selectName();
        List<RpSuccess> rpSuccesses = rpSuccessMapper.selectName();

        List<String> list = new ArrayList<>();

        rpSendFails.forEach(e->{
            list.add(e.getUserModel());
        });

        rpSuccesses.forEach(e->{
            if(!list.contains(e.getUserModel())){
                list.add(e.getUserModel());
            }
        });
        return new Response(StateCode.SEARCH_SUCCESS,(long)list.size(),list);
    }
}
